﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace TortoisePlugin.Settings
{
    public partial class SettingsControl : Common.Controls.SettingsControl
    {
        public override string this[string key]
        {
            get
            {
                switch (key.ToUpperInvariant())
                {
                    case "TORTOISE":
                        return Tortoise.ToString();
                }

                return null;
            }

            set
            {
                switch (key.ToUpperInvariant())
                {
                    case "TORTOISE":
                        {
                            int newValue;
                            if (int.TryParse(value, out newValue))
                                Tortoise = newValue;
                            break;
                        }
                }
            }
        }

        public int Tortoise
        {
            get { return cboTortoise.SelectedIndex; }
            set { cboTortoise.SelectedIndex = value; }
        }

        [Browsable(true)]
        public event EventHandler DefaultTortoiseClick
        {
            add
            {
                btnDefaultTortoise.Click += value;
            }

            remove
            {
                btnDefaultTortoise.Click -= value;
            }
        }

        public SettingsControl()
        {
            InitializeComponent();

            ttTortoise.SetToolTip(cboTortoise, Resources.StringResources.tooltipTortoise);
        }

        public void ResetTest()
        {
            foreach (Control c in grpTortoise.Controls)
            {
                if (c is PictureBox)
                {
                    grpTortoise.Controls.Remove(c);
                }
            }
        }

        public void Success(int step)
        {
            TestResult(step, Common.Resources.CommonResources.ImageTestSuccess);
        }

        public void Fail(int step)
        {
            TestResult(step, Common.Resources.CommonResources.ImageTestFail);
        }

        public void TestResult(int step, Bitmap bmp)
        {
            Point position = new Point();
            Control control = null;

            switch (step)
            {
                case 0:
                    control = cboTortoise;
                    break;
            }

            if (null != control)
            {
                position = new Point(control.Right + 2, control.Top);

                PictureBox pb = new PictureBox();
                pb.Image = bmp;
                pb.Size = new System.Drawing.Size(20, 20);
                pb.Location = position;

                grpTortoise.Controls.Add(pb);
            }
        }
    }
}
